home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / cbzone / cbzone.z / cbzone.tar / c_parseopts.c < prev    next >
C/C++ Source or Header  |  1991-01-28  |  5KB  |  215 lines

  1. #include "c_includes.h"
  2. /*
  3.  * cbzone_parseopts.c
  4.  *  -- Todd W Mummert, December 1990, CMU
  5.  *
  6.  * RCS Info
  7.  *  $Header: c_parseopts.c,v 1.1 91/01/12 02:03:36 mummert Locked $
  8.  *
  9.  * Parse the options, read the MOTD, etc...
  10.  *
  11.  * The prints in these routines will never go the game window, as it
  12.  * does not exist.  cbzone cannot be backgrounded from the beginning
  13.  * since the execl will block on tty output.  To allow this we create
  14.  * yet another flag that specifies that motd is not to be read.
  15.  */
  16.  
  17. int pager(file)
  18.      char* file;
  19. {
  20.   char buf[100], *pager, *getenv();
  21.   static char defaultpager[] = PAGER;
  22.   FILE *f;
  23.  
  24.   if ((pager = getenv("PAGER")) == NULL)
  25.     pager = defaultpager;
  26.   sprintf(buf,"%s%s",TANKDIR,file);
  27.   if ((f=fopen(buf,"r")) != NULL) {
  28.     fclose(f);
  29.     switch (fork()) {
  30.     case 0:
  31.       execl(pager,pager,buf,0);
  32.       fprintf(stderr,"Exec of %s failed\n", pager);
  33.       return 1;
  34.     case -1:
  35.       fprintf(stderr,"Unable to fork process\n");
  36.       return 1;
  37.     default:
  38.       wait(0);
  39.       break;
  40.     }
  41.   }
  42.   else {
  43.     fprintf(stderr,"File %s not found or unreadable.\n", buf);
  44.     return 1;
  45.   }
  46.   return 0;
  47. }
  48.  
  49. int getoptionint(s)
  50.      char *s;
  51. {
  52.   char rest[100];
  53.   int num;
  54.   
  55.   if (sscanf(s, "%d%s", &num, rest) != 1) {
  56.     printf("Error in optional argument %s; use -help for help.\n",
  57.            s);
  58.     exit(0);
  59.   }
  60.   return(num);
  61. }
  62.  
  63. /*
  64.  * the following routine may be called in one of two ways...
  65.  *  either w/ the display set or without...if without, then we
  66.  *  will need to parse all the options...otherwise the resources should
  67.  *  have taken care of most of them for us.
  68.  *
  69.  *  now even if the display is set, we may get options that were
  70.  *  ambiguous.
  71.  *
  72.  *  since options are more than one letter, we can't use getopt.
  73.  */
  74. #define MAXOPTIONS 16
  75. #define OPTIONINT 7
  76. void parseopt(argc, argv, status)
  77.      int argc;
  78.      char* argv[];
  79.      Bool status;
  80. {
  81.   int i;
  82.   Bool early_exit = False;
  83.  
  84.   static char* optionnames[] = {
  85.     "-xrm", "-delay", "-blocks", "-landers", "-tanks", "-missiles",
  86.     "-salvos", "-coptersonly", "-quiet", "-scores", "-original",
  87.     "-version", "-help", "-nooutput", "-mono", "-cursor",
  88.     "-defaultcolormap", "-nofullscreen"};
  89.  
  90.   for (argc--, argv++; argc>0; argc--, argv++) {
  91.     for (i=0; i<MAXOPTIONS; i++)
  92.       if (!strncmp(*argv,optionnames[i],strlen(*argv)))
  93.         break;
  94.     if (i < OPTIONINT) {
  95.       argc--; argv++;
  96.     }
  97.     switch(i) {
  98.     case 0:                     /* xrm */
  99.       break;
  100.     case 1:                     /* delay */
  101.       opt->delay = getoptionint(*argv);
  102.       break;
  103.     case 2:                     /* blocks*/
  104.       opt->mblocks = getoptionint(*argv);
  105.       break;
  106.     case 3:                     /* landers */
  107.       opt->mlanders = getoptionint(*argv);
  108.       break;
  109.     case 4:                     /* tanks */
  110.       opt->mtanks = getoptionint(*argv);
  111.       break;
  112.     case 5:                     /* missiles */
  113.       opt->mmissiles = getoptionint(*argv);
  114.       break;
  115.     case 6:                     /* salvos */
  116.       opt->msalvos = getoptionint(*argv);
  117.       break;
  118.     case 7:                     /* copter practice */
  119.       opt->copters = True;
  120.       break;
  121.     case 8:                     /* quiet mode */
  122.       opt->loud = False;
  123.       break;
  124.     case 9:                     /* scores only */
  125.       opt->scores = True;
  126.       break;
  127.     case 10:                    /* original */
  128.       opt->original = True;
  129.       break;
  130.     case 11:                    /* version */
  131.       opt->version = True;
  132.       break;
  133.     case 12:                    /* help */
  134.       opt->help = True;
  135.       break;
  136.     case 13:                    /* nooutput */
  137.       opt->output = False;
  138.       break;
  139.     case 14:                    /* monocolor */
  140.       opt->mono = True;
  141.       break;
  142.     case 15:                    /* cursor */
  143.       opt->cursor = True;
  144.       break;
  145.     case 16:                    /* default colormap */
  146.       opt->defaultcolormap = True;
  147.       break;
  148.     case 17:                    /* fullscreen */
  149.       opt->fullscreen = False;
  150.       break;
  151.     }
  152.   }
  153.  
  154.   if (opt->scores || opt->help || opt->version)
  155.     early_exit = True;
  156.   
  157.   if (opt->output) {
  158.     pager("cbzone.motd");
  159.  
  160.     if (opt->scores)
  161.       scores(-1);
  162.  
  163.     if (opt->version) 
  164.       printf("\nVersion \"%s\"\n", VERSION);
  165.     
  166.     if (opt->help && pager("cbzone.help"))
  167.       printf("Sorry help information not available.\n");
  168.   }
  169.  
  170.   if (early_exit)
  171.     exit(0);
  172.  
  173.   if (!status)
  174.     return;
  175.  
  176.   if (opt->copters)
  177.     opt->mtanks = 0;
  178.  
  179.   if (opt->original) {
  180.     opt->mblocks = 8;
  181.     opt->copters = False;
  182.     opt->mlanders = 1;
  183.     opt->mmissiles = 1;
  184.     opt->mtanks = 1;
  185.     opt->practice = True;
  186.     opt->msalvos = 1;
  187.   }
  188.  
  189.   opt->menemies = (opt->mtanks > opt->mmissiles ?
  190.                    opt->mtanks : opt->mmissiles);
  191.   if (!opt->menemies) {
  192.     printf("Must have at least one missile or tank.\n");
  193.     exit(1);
  194.   }
  195.  
  196.   if (opt->msalvos == -1)
  197.     opt->msalvos = opt->menemies;
  198.   opt->mobjects = opt->mblocks + opt->mlanders + 2*opt->menemies +
  199.     opt->msalvos + 1;
  200.   opt->estart = 1;
  201.   opt->lstart = opt->estart + opt->menemies;
  202.   opt->sstart = opt->lstart + opt->mlanders;
  203.   opt->bstart = opt->sstart + opt->menemies + opt->msalvos;
  204.  
  205.   if (opt->mmissiles == MMISSILES &&
  206.       opt->mtanks == MTANKS &&
  207.       opt->mlanders == MLANDERS &&
  208.       opt->mblocks == MBLOCKS &&
  209.       opt->delay <= DELAY &&
  210.       opt->msalvos == opt->menemies)
  211.     opt->practice = False;
  212.   else
  213.     opt->practice = True;
  214. }
  215.